<?php
// 注册
$loginName = $_REQUEST["loginName"];
$pwd = $_REQUEST["pwd"];
$nickname = $_REQUEST["nickname"];
// TODO: 数据格式的验证

$conn = mysqli_connect("localhost:3306","root","","php");
// 验证登录名是否重复
$sql = "select login_name from t_user where login_name = '$loginName'";
$result = mysqli_query($conn, $sql);
if($result){
    if(mysqli_num_rows($result)){
        echo "<h2 style='color:red;' >登录名重复</h2>";
        mysqli_close($conn);
        return;
    }
} else {
    echo "<h2>验证登录名 sql 执行错误</h2>";
}

// 注册的数据库操作
$sql = "insert into t_user (login_name,pwd,nickname,create_date) "
. " values ('$loginName', password('$pwd'), '$nickname', now())";
$result = mysqli_query($conn, $sql);
if($result){
    // 成功, 跳转到登录页面
    header("location:/week_homework/login.html");
}
else {
    // 失败
    echo "注册失败:" . mysqli_error($conn);
}
mysqli_close($conn);

?>